package cn.edu.ctbu.scoremg.api;

import cn.edu.ctbu.scoremg.constant.REnum;
import cn.edu.ctbu.scoremg.entity.Student;
import cn.edu.ctbu.scoremg.exception.RException;
import cn.edu.ctbu.scoremg.service.StudentService;
import cn.edu.ctbu.scoremg.util.RUtil;
import cn.edu.ctbu.scoremg.vo.QueryObj;
import cn.edu.ctbu.scoremg.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/api/student")
@Slf4j
public class StudentApiController {
    @Autowired
    private StudentService studentService;
    public StudentApiController(StudentService studentService) {this.studentService=studentService;}
    @GetMapping("/list")
    public R<List<Student>> findAll() {
        List<Student> students = studentService.findAll();
        return RUtil.success(students);
    }
    @GetMapping("/{id}")
    public R<Student> findById(@PathVariable Integer id) {
        Student student = studentService.findById(id);
        return RUtil.success(student);
    }

    @PostMapping("/add")
    public R<Student> add(Student student) {
        return RUtil.success(studentService.insert(student));
    }

    @PutMapping("/update")
    public R<Student> update(Student student) {
        return RUtil.success(studentService.update(student));
    }

    @DeleteMapping("/deleteId/{ids}")
    public R<Student> delete(@PathVariable String ids) {
        try {
            List<Integer> studIds = Stream.of(ids.split(","))
                    .map(Integer::parseInt).collect(Collectors.toList());
            studentService.deleteByIds(studIds);
        }catch (Exception e){
            return  RUtil.error(1,e.getMessage());
        }
        return RUtil.success();
    }
    @PostMapping("/getbypage")
    public R<Page<Student>> getPage(@RequestBody QueryObj<Student> qObj) {
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Integer pageIndex = 0;
        Integer pageSize = 10;
        if(qObj==null){
            Pageable pageable = PageRequest.of(pageIndex,pageSize,sort);
            Page<Student> students=studentService.getAll(pageable);
            return RUtil.success(students.getContent(),students.getTotalElements());
        }else {
            pageIndex = qObj.getPage() - 1;
            pageSize = qObj.getLimit();

            if (qObj.getData() instanceof Student) {
                Student student = (Student) qObj.getData();
                Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);

                ExampleMatcher matcher = ExampleMatcher.matching()
                        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withIgnoreNullValues();//忽略空值
                Example<Student> example = Example.of(student, matcher);
                Page<Student> students = studentService.getAll(example, pageable);
                return RUtil.success(students.getContent(), students.getTotalElements());
            } else {
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }
}
